import { Injectable } from '@angular/core';
import { ShapeBoxBase } from '../model/component-model/component-related/box-base';
import { CommonUtil } from '../../util/common-util';
import { inspect } from 'util';
import { EventEmitter } from 'events';
import { Operation } from '../model/operation.model';
import { PointBase } from '../model/point-model/point-related/point-base';
import { RootContainer } from '../model/component-model/container-model';

@Injectable()
export class BoxService {
    private static instance: BoxService;
    private operation: Operation = null;
    private operating: Operation = null;
    public boxs: ShapeBoxBase[] = [];
    public container: ShapeBoxBase;
    public selectedBox: ShapeBoxBase[] = [];
    private constructor() { }
    //鼠标按下
    public select(e) {
        if (this.operating) {
            if(this.operating instanceof ShapeBoxBase){
                this.selectBox(e);
            }else if(this.operating instanceof PointBase){
                this.selectPoint(e);
            }
        } else {
            this.container.select(e);
        }
    }
    //选中组件
    public selectBox(e){
        let box: ShapeBoxBase = <ShapeBoxBase>this.operating;
        if (!this.selectedBox.includes(box)) {
            if (!e.ctrlKey) {
                this.allOut();
                this.selectedBox = [];
            }
            this.selectedBox.push(box);
        }
        this.boxs[0].select(e);
        this.operation = this.boxs[0];
    }
    //选中点
    public selectPoint(e) {
        let point = <PointBase>this.operating;
        point.select(e);
        if (this.cursor() != "default") {
            this.operation = point;
        }
    }
    //判断是否在选中状态
    public ifSelected(box: ShapeBoxBase) {
        box.selected;
    }
    //取消所有组件的选中状态
    public allOut() {
        for (let box of this.boxs) {
            if (box.selected) {
                box.out();
            }
        }
    }
    //根据id取组件
    public getBoxById(id: string): ShapeBoxBase {
        for (let box of this.boxs) {
            if (box.getId() == id) {
                return box;
            }
        }
    }
    //id校验
    public generateId(id?: string) {
        if (!id) {
            return CommonUtil.getTime();
        }
        for (let box of this.boxs) {
            if (box.getId() == id) {
                return false;
            }
        }
        return true;
    }
    //获取z-index
    public getIndex() {
        return this.container.style["z-index"] + this.boxs.length;
    }
    //鼠标移动
    move(e) {
        if (this.operation) {
            this.operation.move(e);
            return;
        }
        if (this.operating && !this.operating["selected"]) {
            this.operating.out();
        }
        this.operating = null;
        this.cursor("default");
        //select选框各点情况
        if(this.selectedBox.length > 0 && this.movePoints(this.boxs[0].points, e)){
            return;
        }
        for (let i = this.boxs.length - 1; i > 0; i--) {
            let op: ShapeBoxBase = this.boxs[i];
            if (op.canvasMove(e)) {
                this.operating = op;
                this.operating.in();
                this.movePoints(op.points, e);
                break;
            }
        }
    }
    movePoints(pointsA: PointBase[][], e) {
        for (let points of pointsA) {
            for (let point of points) {
                if (point.canvasMove(e)) {
                    this.operating = point;
                    return true;
                }
            }
        }
        return false;
    }
    //结束
    finish() {
        this.boxs[0].render();
        this.operation = null;
    }
    //修改鼠标样式
    cursor(cursor?: string) {
        if (!cursor) {
            return this.container.style.cursor;
        }
        this.container.style.cursor = cursor;
    }
    //获取service实例
    static getService() {
        if (this.instance == null) {
            this.instance = new BoxService();
        }
        return this.instance;
    }
}
